local sk__zhubao = fk.CreateSkill {

  name = "sk__zhubao",

  tags = {  },

}



sk__zhubao:addEffect(fk.AfterCardsMove, {
  name = "sk__zhubao",
  anim_type = "drawcard",
  can_trigger = function(self, event, target, player, data)
    if player:hasSkill(sk__zhubao.name) and player.phase ~= Player.NotActive and not player:isKongcheng() then
      local tos = {}
      for _, move in ipairs(data) do
        if move.from and move.from ~= player and not table.contains(player:getTableMark("sk__zhubao-turn"), data.from.id) then
          local yes = false
          for _, info in ipairs(move.moveInfo) do
            if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
              if table.find(player:getCardIds("h"), function(id)
                return Fk:getCardById(id).suit == Fk:getCardById(info.cardId).suit and
                  Fk:getCardById(info.cardId).suit ~= Card.NoSuit
                end) then
                yes = true
              end
            end
          end
          if yes then
            table.insertIfNeed(tos, move.from.id)
          end
        end
      end
      if #tos > 0 then
        event:setCostData(self, tos)
        return true
      end
    end
  end,
  on_trigger = function(self, event, target, player, data)
    self.cancel_cost = false
    local tos = event:getCostData(self)
    for _, id in ipairs(tos) do
      if not player:hasSkill(sk__zhubao.name) or self.cancel_cost then return end
      if not table.contains(player:getTableMark("sk__zhubao-turn"), id) then
        event:setCostData(self, id)
        self:doCost(event, target, player, data)
      end
    end
  end,
  on_cost = function(self, event, target, player, data)
    if player.room:askForSkillInvoke(player, sk__zhubao.name) then
      return true
    end
    self.cancel_cost = true
  end,
  on_use = function(self, event, target, player, data)
    local mark = player:getTableMark("sk__zhubao-turn")
    table.insert(mark, event:getCostData(self))
    player.room:setPlayerMark(player, "sk__zhubao-turn", mark)
    local suits = {}
    for _, move in ipairs(data) do
      if move.from and move.from.id == event:getCostData(self) then
        for _, info in ipairs(move.moveInfo) do
          if info.fromArea == Card.PlayerHand or info.fromArea == Card.PlayerEquip then
            if Fk:getCardById(info.cardId).suit ~= Card.NoSuit then
              table.insertIfNeed(suits, Fk:getCardById(info.cardId).suit)
            end
          end
        end
      end
    end
    local n = 0
    for _, id in ipairs(player:getCardIds("h")) do
      if table.contains(suits, Fk:getCardById(id).suit) then
        n = n + 1
      end
    end
    player:drawCards(n, sk__zhubao.name)
  end,
})

return sk__zhubao